package com.ruoyi.livesass.api.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.manager.lbs.service.LbsZbjlService;
import com.cyl.manager.yee.domain.YeeMerchantSif;
import com.cyl.manager.yee.mapper.YeeMerchantSifMapper;
import com.cyl.manager.yee.service.YeeMerchantSifService;
import com.google.common.collect.Maps;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.livesass.api.domain.LbsAliyunAmountSplit;
import com.ruoyi.livesass.api.domain.LbsCloudResource;
import com.ruoyi.livesass.api.domain.LbsZbll;
import com.ruoyi.livesass.api.domain.LbsZbrqsj;
import com.ruoyi.livesass.api.mapper.LbsAliyunAmountSplitMapper;
import com.ruoyi.livesass.api.mapper.LbsCloudResourceMapper;
import com.ruoyi.livesass.api.mapper.LbsZbllMapper;
import com.ruoyi.livesass.api.service.LbsAliyunAmountSplitService;
import com.ruoyi.livesass.api.service.LbsCloudResourceService;
import com.ruoyi.livesass.api.service.LbsZbllService;
import com.ruoyi.livesass.api.sync.LiveActivityDataSynService;
import com.ruoyi.livesass.api.sync.LiveUserTimeDataToDaySynService;

import cn.hutool.core.util.StrUtil;


/**
* @ClassName: ManageController 
* @Description: TODO(管理端下发控制器) 
* @author 晏大兵 
* @date 2025年3月22日 下午1:32:09 
*/
@RestController
@RequestMapping("/manageSystem")
public class ManageController extends BaseController {
	
	
	@Value("${custom.manageSystemIp:0}")
	private String manageIp;
	
	@Autowired
	LbsAliyunAmountSplitMapper lbsAliyunAmountSplitMapper;
	
	@Autowired
	LbsAliyunAmountSplitService lbsAliyunAmountSplitService;
	
	@Autowired
	LbsCloudResourceMapper lbsCloudResourceMapper;
	
	@Autowired
	LbsCloudResourceService lbsCloudResourceService;
	
	@Autowired
	YeeMerchantSifService yeeMerchantSifService;
	
    @Autowired
    YeeMerchantSifMapper yeeMerchantSifMapper;

    @Autowired
    LbsZbllMapper lbsZbllMapper;
    
    @Autowired
    LbsZbllService lbsZbllService;
    
	
	
	/**
	* @Title: synAliyunResource 
	* @Description: TODO(同步阿里云资源费用数据) 
	* @param @param ActivityID
	* @param @param EventType
	* @param @param Status
	* @param @param Timestamp
	* @param @param Sign
	* @param @param SubAccountName
	* @param @param RequestUuid
	* @param @return    设定文件 
	* @return ResponseEntity<String>    返回类型 
	* @throws
	 */
    @PostMapping("/synAliyunResource")
    public ResponseEntity<String> synAliyunResource(@RequestBody LbsAliyunAmountSplit lbsAliyunAmountSplit, HttpServletRequest request) {
        String ipAddress = request.getRemoteAddr();
        
        
        if(!ipAddress.equals(manageIp)) {
        	System.out.println("白名单IP | " + manageIp );
        	System.out.println("请求IP | " + ipAddress);
        	return ResponseEntity.ok("系统不合法!");
        }
        
    	QueryWrapper<LbsAliyunAmountSplit> queryAliyunAmountSplit = new QueryWrapper<>();
    	queryAliyunAmountSplit.eq("gsrq", lbsAliyunAmountSplit.getGsrq());
        
        //首先删除之前的数据
        lbsAliyunAmountSplitMapper.delete(queryAliyunAmountSplit);
        //然后保存最新的一条
        lbsAliyunAmountSplitService.insert(lbsAliyunAmountSplit);
        
        
        //将费用同步到LbsCloudResource
    	QueryWrapper<LbsCloudResource> queryLbsCloudResource = new QueryWrapper<>();
    	queryLbsCloudResource.eq("gsrq", lbsAliyunAmountSplit.getGsrq());
    	lbsCloudResourceMapper.delete(queryLbsCloudResource);
    	
        LbsCloudResource lbsCloudResource = new LbsCloudResource(); //删除之前的
        lbsCloudResource.setGsrq(lbsAliyunAmountSplit.getGsrq());
        lbsCloudResource.setAlb(lbsAliyunAmountSplit.getSlbGrossAmount());
        lbsCloudResource.setCdn(lbsAliyunAmountSplit.getCdnGrossAmount());
        lbsCloudResource.setDb(lbsAliyunAmountSplit.getDbGrossAmount());
        lbsCloudResource.setRedis(lbsAliyunAmountSplit.getRedisGrossAmount());
        lbsCloudResource.setSae(lbsAliyunAmountSplit.getSaeGrossAmount());
        lbsCloudResource.setCdn(lbsAliyunAmountSplit.getCdnGrossAmount());
        lbsCloudResource.setWaf(lbsAliyunAmountSplit.getWafGrossAmount());
        lbsCloudResource.setQtfy(lbsAliyunAmountSplit.getOtherGrossAmount());
        
        
        double gwll = lbsAliyunAmountSplit.getCdtGrossAmount().doubleValue();
        gwll = gwll + lbsAliyunAmountSplit.getNatGrossAmount().doubleValue();
        gwll = gwll + lbsAliyunAmountSplit.getEipGrossAmount().doubleValue();
        gwll = gwll + lbsAliyunAmountSplit.getOssGrossAmount().doubleValue();
        gwll = gwll + lbsAliyunAmountSplit.getCasGrossAmount().doubleValue();
        gwll = gwll + lbsAliyunAmountSplit.getSmsGrossAmount().doubleValue();
        
        lbsCloudResource.setGwll(new BigDecimal(gwll));  //公网流量包含 对象存储 nat网关 cdt云传输 eip弹性ip  cas证书 sms短信
        
        
        double ecs = lbsAliyunAmountSplit.getEcsGrossAmount().doubleValue();
        ecs = ecs + lbsAliyunAmountSplit.getNasGrossAmount().doubleValue();
        lbsCloudResource.setEcs(new BigDecimal(ecs)); //ECS需要加上nas费用
        
        
        lbsCloudResource.setSubname(lbsAliyunAmountSplit.getDomain());
        lbsCloudResource.setZkje(lbsAliyunAmountSplit.getMount());
        lbsCloudResource.setYsje(lbsAliyunAmountSplit.getPretaxGrossAmount());
        
        lbsCloudResourceService.insert(lbsCloudResource);
    	
    	return ResponseEntity.ok("同步完成!");
    }
		
    
    
   /**
   * @Title: deductAliyunResourceMount 
   * @Description: TODO(扣费) 
   * @param @param gsrq
   * @param @param kfje
   * @param @return    设定文件 
   * @return ResponseEntity<String>    返回类型 
   * @throws
    */
    @PostMapping("/deductMount")
    public ResponseEntity<String> deductAliyunResourceMount(String gsrq, double kfje, Long kfbh, String jsfl, String jsyy, HttpServletRequest request) {
    	
    	String ipAddress = request.getRemoteAddr();
        
        if(!ipAddress.equals(manageIp)) {
        	System.out.println("白名单IP | " + manageIp );
        	System.out.println("请求IP | " + ipAddress);
        	return ResponseEntity.ok("系统不合法!");
        }
        
        
    	//String jsfl = "24"; //结算分类24 云资源费用
    	//String jsyy = "云计算资源扣费月度["+ gsrq +"]扣费.";
    	YeeMerchantSif yeeMerchantSif = yeeMerchantSifService.startSysSif(new BigDecimal(kfje), jsyy, jsfl, kfbh);
    	
    	int cwspSysSatrtResult = yeeMerchantSifService.cwspSysSatrt(yeeMerchantSif.getId(), yeeMerchantSif.getMacAddress());
    	Map<String, Object> responseMap = Maps.newHashMap();
    	responseMap.put("cwspSysSatrtResult", cwspSysSatrtResult);
    	responseMap.put("yeeMerchantSif", yeeMerchantSif);
    	
    	
    	return ResponseEntity.ok(JSONObject.toJSONString(responseMap));
    }
    
    
    /**
     * @Title: syncHsZbll 
     * @Description: TODO(同步火山直播流量) 
     * @param @return    设定文件 
     * @return ResponseEntity<String>    返回类型 
     * @throws
      */
      @GetMapping("/syncHsZbll")
      public ResponseEntity<String> syncHsZbll(HttpServletRequest request) {
      	
      	String ipAddress = request.getRemoteAddr();
          
        if(!ipAddress.equals(manageIp)) {
          System.out.println("白名单IP | " + manageIp );
          System.out.println("请求IP | " + ipAddress);
          //return ResponseEntity.ok("系统不合法!");
        }
        
        Map<String, Map<String,Object> > resultData = Maps.newHashMap();
        //lbsZbllService.selectList(null, null)
        
        QueryWrapper<LbsZbll> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        
        
        List<LbsZbll> lbsZbllList = lbsZbllMapper.selectList(qw);
        
        for(LbsZbll item : lbsZbllList) {
        	if(!StrUtil.isBlankIfStr(item.getGsrq())) {
        		String gsyf =  item.getGsrq().substring(0, 7);
        		Map<String,Object> resultItem;
        		
        		if(resultData.containsKey(gsyf)) {
        			resultItem = resultData.get(gsyf);
        		}else {
        			resultItem = Maps.newHashMap();
        		}
        		
        		if(resultItem.containsKey("zbll")) {
        			BigDecimal re_zbll = (BigDecimal) resultItem.get("zbll");
        			BigDecimal sum = re_zbll.add(item.getZbll());
        			resultItem.put("zbll", sum);
        		}else {
        			resultItem.put("zbll", item.getZbll());
        		}
        		
        		if(resultItem.containsKey("zbllfy")) {
        			BigDecimal re_zbllfy = (BigDecimal) resultItem.get("zbllfy");
        			BigDecimal sum = re_zbllfy.add(item.getZbllfy());
        			resultItem.put("zbllfy", sum);
        		}else {
        			resultItem.put("zbllfy", item.getZbllfy());
        		}
        		
        		if(resultItem.containsKey("gkrs")) {
        			Long re_gkrs = (Long) resultItem.get("gkrs");
        			re_gkrs = re_gkrs + item.getGkrs();
        			resultItem.put("gkrs", re_gkrs);
        		}else {
        			resultItem.put("gkrs", item.getGkrs());
        		}
        		
        		resultData.put(gsyf, resultItem);
        		
        	}
        	
        }
      	
      	
      	return ResponseEntity.ok(JSONObject.toJSONString(resultData));
      }
		


}
